package com.missilike.api.config;

import com.missilike.core.aop.GlobalExceptionHandler;
import com.missilike.core.config.*;
import com.missilike.core.domain.AliyunProperties;
import com.missilike.core.domain.JPushProperties;
import com.missilike.core.domain.WechatProperties;
import com.missilike.core.domain.alipay.AlipayProperties;
import com.missilike.core.service.IAlipayService;
import com.missilike.core.service.IJPushService;
import com.missilike.core.service.IOSSServcice;
import com.missilike.core.service.impl.*;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Configuration
@Import({GlobalExceptionHandler.class, ThirdConfig.class, IMConfig.class, AliyunConfig.class, AlipayConfig.class, GoodsConfig.class, JPushConfig.class})
public class AppConfig {

    @Bean
    public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory componentsClientHttpRequestFactory,
                                     List<HttpMessageConverter<?>> messageConverters) {
        RestTemplate restTemplate = new RestTemplate(componentsClientHttpRequestFactory);
        restTemplate.setMessageConverters(messageConverters);
        return restTemplate;
    }

    @Bean
    public WechatService wechatService(RedisTemplate redisTemplate, RestTemplate restTemplate,WechatProperties wechatProperties){
        return new WechatService(restTemplate,redisTemplate,wechatProperties);
    }

    @Bean
    public IOSSServcice iossServcice(AliyunProperties aliyunProperties){
        return new OSSService(aliyunProperties);
    }

    @Bean
    public IAlipayService alipayService(AlipayProperties alipayProperties){
        return new AlipayService(alipayProperties);
    }

    @Bean
    public IJPushService jPushService(JPushProperties jPushProperties){
        return new JPushService(jPushProperties);
    }

    @Bean
    public HttpComponentsClientHttpRequestFactory componentsClientHttpRequestFactory(
            @Value("${rest.call.connect.timeout}") int connectTimeout,
            @Value("${rest.call.read.timeout}") int readTimeout){
        HttpClient httpClient;
        httpClient = HttpClients.custom().build();
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        httpComponentsClientHttpRequestFactory.setConnectTimeout(connectTimeout);
        httpComponentsClientHttpRequestFactory.setReadTimeout(readTimeout);
        return httpComponentsClientHttpRequestFactory;
    }
}
